<template>
  <div class="pad_10">
    <el-form ref="queryRef" :inline="true" label-width="68px">
      <el-form-item>
        <el-button v-if="look" @click="handleQuery">保存</el-button>
        <el-button @click="Cancel">取消</el-button>
        <el-button v-if="look" :disabled="!route.query.id" @click="publishFun">发布</el-button>
        <el-button v-if="look" :disabled="!route.query.id" @click="notPublishedFun">下架</el-button>
        <!-- <el-button :disabled="!route.query.id" @click="auditFun">审核</el-button> -->
      </el-form-item>
    </el-form>
    <el-divider />
    <SetUserSelect
      :key="keys"
      ref="setUserSelect"
      class="queryForm"
      :labelwidth="100"
      :isShowUserBtn="false"
      :buttonIsShow="false"
      :formData="bill"
      :queryParams="datas"
      @selChangeFun="changeFun"
      @selChangeFuncom="selChangeFuncomF"
    ></SetUserSelect>
  </div>
</template>
<script setup lang="ts">
import { ref, getCurrentInstance, onBeforeUnmount, onMounted } from "vue";
import { ItemType } from "@/components/types/types"
import { addhall, hallId, eitHhall, publish, notPublished,  } from "@/api/taskList";
import data from './data';
import { useRoute } from "vue-router";
import { el } from "node_modules/element-plus/lib/locale";
const {  datas, researchItem, compList, academic } = data;
const bill = ref<ItemType[]>([
  
  {
    label: '名称',
    message: '请输入名称',
    required: true,
    model: 'name',
    placeholder: '请输入',
    type: 'input'
  },
  { 
    label: '年份',
    model: 'year',
    placeholder: '请选择',
    type: 'select',
    dict: 'set_year_time',
    selectData: []
  },
  { 
    label: '月份',
    model: 'month',
    placeholder: '请选择',
    type: 'select',
    dict: 'set_moon_time',
    selectData: []
  },
  {
    label: '类型',
    model: 'type',
    placeholder: '请选择',
    type: 'select',
    required: true,
    disabled: false,
    message: '请选择品类',
    dict: 'timed_task_type',
    selectData: []
  },
  {
    label: '任务名称',
    message: '请输入名称',
    required: true,
    model: 'taskName',
    placeholder: '请输入',
    type: 'input'
  },
  {
    label: '省份',
    model: 'province',
    placeholder: '请选择',
    type: 'select',
    url: 'base/city/get-province',
    valueName: 'cityname',
    required: true,
    disabled: false,
    message: '请选择类别',
    selectData: []
  },
  {
    label: '数量',
    message: '请输入数量',
    required: true,
    model: 'quantity',
    placeholder: '请输入',
    type: 'input',
    regexp: 'number',
    maxlength: 8,
  },
  {
    label: '已申请数量',
    message: '请输入已申请数量',
    required: false,
    disabled: true,
    model: 'appliedQty',
    placeholder: '请输入',
    type: 'input'
  },
  {
    label: '开始时间：',
    model: 'startTime',
    required: true,
    placeholder: '选择时间',
    type: 'beginTime',
    timeType: 'datetime',
    message: '请选择开始时间',
    setTime: 'setTime',
  },
  {
    label: '截止时间：',
    model: 'endTime',
    required: true,
    placeholder: '选择时间',
    type: 'beginTime',
    timeType: 'datetime',
    message: '请选择截止时间',
    setTime: 'setTimemin',
  },
  {
    label: '状态',
    model: 'status',
    placeholder: '请选择',
    type: 'select',
    required: false,
    disabled: true,
    dict: 'timed_task_status',
    message: '请选择状态',
    selectData: []
  },
  {
    label: '积分/个',
    message: '请输入积分',
    required: true,
    model: 'scorePer',
    placeholder: '请输入',
    type: 'input'
  },
  {
    label: '品种',
    model: 'drugId',
    placeholder: '请选择',
    type: 'select',
    required: true,
    disabled: false,
    url: '/base/drug/list',
    message: '请选择品种',
    selectData: []
  },
  {
    label: '项目类型',
    model: 'projectType',
    placeholder: '请选择',
    type: 'select',
    required: true,
    disabled: false,
    message: '请选择项目类型',
    dict: 'pro_ject_type',
    selectData: []
  }
])
onBeforeUnmount(() => {
  datas.value.type = ''
  datas.value.drugOwner = ''
  window.sessionStorage.setItem('setDateTimedTask', JSON.stringify(datas.value))
})
onMounted(() => {
  if (window.sessionStorage.getItem('setDateTimedTask')) {
    let dateNew = JSON.parse(window.sessionStorage.getItem('setDateTimedTask'))
    datas.value = dateNew
    window.sessionStorage.removeItem('setDateTimedTask')
  } else {
    datas.value = {
      appliedQty: 0,
      status: '1',
      province: '',
    }
  }
})

const { proxy } = getCurrentInstance();
let proxys: any = proxy;
const route: any = useRoute();
let keys = ref<any>("");
const billNew:any = JSON.parse(JSON.stringify(bill.value))



const handleQuery = function (): void {
  proxys.$refs.setUserSelect.submitForm().then((valid) => {
    if(route.query.copy){
      addhall({
        ...valid,
        id: undefined,
        status: 1,
        appliedQty: 0
      }).then(() => {
        proxys.$modal.msgSuccess(`新增成功`);
      })
      return
    }
    (!route.query.id ? addhall : eitHhall)(valid).then(() => {
      proxys.$modal.msgSuccess(`${route.query.id ? "修改" : "新增"}成功`);
      Cancel();
    });
  });
};
const hallIdFun = () => {
  hallId(route.query.id).then((res) => {
    res.data.status = "" + res.data.status;
    if(route.query.copy){
      res.data.status = "1"
    }
    res.data.type = "" + res.data.type;
    res.data.setTime = res.data.startTime
    res.data.setTimemin = res.data.endTime
    datas.value = res.data;
    if (res.data.type == 2) {
      bill.value = [...billNew, ...researchItem.value]
      keys.value = new Date().getTime();
      if (res.data.drugOwner == 2) {
        bill.value = [...billNew, ...researchItem.value, ...compList.value]
        keys.value = new Date().getTime();
      } else {
        bill.value = [...billNew, ...researchItem.value]
        keys.value = new Date().getTime();
      }
    } else if (res.data.type == 3 || res.data.type == 4) {
      bill.value = [...billNew, ...academic.value]
      keys.value = new Date().getTime();
    } else {
      bill.value = billNew
      keys.value = new Date().getTime();
    }

    

  })
}
let look:any = ref(true)
if (route.query.look) {
  look.value = false
}
route.query.id && (hallIdFun());
const changeFun = function (e: any): void {
  bill.value = []
  if (e.model == 'type') {
    if (e.val == 2) {
      bill.value = [...billNew, ...researchItem.value]
      keys.value = new Date().getTime();
    } else if (e.val == 3 || e.val == 4) {
      bill.value = [...billNew, ...academic.value]
      keys.value = new Date().getTime();
    } else {
      bill.value = billNew
      keys.value = new Date().getTime();
    }
  }
};
const selChangeFuncomF = function (e: any): void {
  bill.value = []
  if (e.val == 2) {
    bill.value = [...billNew, ...researchItem.value, ...compList.value]
    keys.value = new Date().getTime();
  } else if (e.val == 3 || e.val == 4) {
    bill.value = [...billNew, ...academic.value]
    keys.value = new Date().getTime();
  } else {  
    bill.value = [...billNew, ...researchItem.value]
    keys.value = new Date().getTime();
  }
};
const Cancel = () => {
  datas.value = {
    appliedQty: 0,
    status: '1',
    province: '',
  }
  const obj = { path: "/timedTask/taskList" };
  proxys.$tab.closeOpenPage(obj);
};
const publishFun = () => {
  if (datas.value.status == '1' || datas.value.status == '3') {
    publish({
      id: route.query.id
    }).then((res) => {
      proxys.$modal.msgSuccess(`发布成功`);
      hallIdFun()
    })
  } else {
    proxys.$modal.msgWarning('未发布跟下架的任务才能发布')
  }
  
}
const notPublishedFun = () => {
  if (datas.value.status == '2') { 
    notPublished({
      id: route.query.id
    }).then((res) => {
      proxys.$modal.msgSuccess(`下架成功`);
      hallIdFun()
    })
  } else {
    proxys.$modal.msgWarning('已发布的任务才能下架')
  }
  
}
</script>
<style scoped>
.pad_10 {
  padding: 10px;
}
.pad_10 .el-form--inline .el-form-item {
  margin: 0;
}
.el-divider--horizontal {
  margin: 10px 0;
}
</style>